<template>
    <div>
        <div style="margin-bottom: 30px">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>新闻后台管理</el-breadcrumb-item>
                <el-breadcrumb-item>新闻分类</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <el-button type="primary" @click="handleAdd" style="margin-bottom: 20px"
            >新增分类 <i class="el-icon-circle-plus-outline"></i
            ></el-button>
        </div>
        <el-table
                :data="tableData"
                border
                stripe
        >
            <el-table-column type="index" label="ID" width="80"></el-table-column>
            <el-table-column
                    prop="label"
                    label="分类名称"
                    width="140"
            ></el-table-column>

            <el-table-column label="操作" width="120" align="center">
                <template v-slot="scope">

                    <el-popconfirm
                            class="ml-5"
                            confirm-button-text="确定"
                            cancel-button-text="我再想想"
                            icon="el-icon-info"
                            icon-color="red"
                            title="您确定删除吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button
                                v-if="scope.row.label!='推荐'"
                                type="danger"
                                size="small"
                                slot="reference"
                        >删除 <i class="el-icon-remove-outline"></i
                        ></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="新增新闻分类" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width="80px" size="small">
                <el-form-item label="分类名称">
                    <el-input v-model="form.label" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {getCategoryList, deleteCategory, addOrUpdateCategory} from "@/api/Category";

export default {
    data() {
        return {
            dialogFormVisible: false,
            tableData: [],
            form: {},
        }
    },
    created() {
        this.getAllLinkList()
    },
    methods: {
        async getAllLinkList() {
            const res = await getCategoryList()
            this.tableData = res
        },
        async del(id) {
            const res = await deleteCategory(id)
            if (res) {
                this.$message.success("删除成功");
                await this.getAllLinkList();
            } else {
                this.$message.error("删除失败");
            }
        },
        async save() {
            console.log(this.form)
            const res = await addOrUpdateCategory(this.form)
            if (res) {
                this.$message.success("保存成功");
                this.dialogFormVisible = false;
                await this.getAllLinkList();
            } else {
                this.$message.error("保存失败");
            }
        },
        handleAdd() {
            this.dialogFormVisible = true;
            this.form = {};
        },
    }
}
</script>

<style>

</style>
